<template>
  <el-dialog :visible.sync = "visible" :title="!dataForm.attrId ? '新增' : '修改'" @close="dialogClose">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="150px">
      <el-form-item label="属性名称" prop="attrName">
        <el-input v-model="dataForm.attrName" placeholder="属性名"></el-input>
      </el-form-item>
      <el-form-item label="属性类型" prop="attrType">
        <el-select v-model="dataForm.attrType" placeholder="请选择">
          <el-option label="规格参数" :value="1"></el-option>
          <el-option label="销售属性" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="值类型" prop="valueType">
        <el-switch
          v-model="dataForm.valueType"
          active-text="允许多个值"
          inactive-text="只能单个值"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :inactive-value="0"
          :active-value="1"
        ></el-switch>
      </el-form-item>
      <el-form-item label="可选值" prop="valueSelect">
        <!-- <el-input v-model="dataForm.valueSelect"></el-input> -->
        <el-select
          v-model="dataForm.valueSelect"
          multiple
          filterable
          allow-create
          placeholder="请输入内容"
        ></el-select>
      </el-form-item>
      <el-form-item label="属性图标" prop="icon">
        <el-input v-model="dataForm.icon" placeholder="属性图标"></el-input>
      </el-form-item>
      <el-form-item label="所属分类" prop="categoryId">
        <category-cascader :catelogPath.sync="catelogPath"></category-cascader>
      </el-form-item>
      <el-form-item label="所属分组" prop="attrGroupId" v-if=" type === 1">
        <el-select v-model="dataForm.attrGroupId" placeholder="请选择">
          <el-option
            v-for="item in attrGroups"
            :key="item.attrGroupId"
            :label="item.attrGroupName"
            :value="item.attrGroupId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="可检索" prop="searchType" v-if=" type === 1">
        <el-switch v-model="dataForm.searchType" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0"></el-switch>
      </el-form-item>
      <el-form-item label="快速展示" prop="showDesc" v-if=" type === 1">
        <el-switch v-model="dataForm.showDesc" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0"></el-switch>
      </el-form-item>
      <el-form-item label="启用状态" prop="enable">
        <el-switch v-model="dataForm.enable" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0"></el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import CategoryCascader from '../common/category-cascader';
  import attrService from '@/api/product/attr.js'
  import attrgroupService from "@/api/product/attrgroup.js";
export default {
  name: "",
  props:{
    type:{
      type: Number,
      default: 1
    }
  },
  components: {CategoryCascader},
  data(){
    return {
      attrGroups: [],
      catelogPath: [],
      dataForm: {
        attrId: 0,
        attrName: "",
        searchType: 0,
        valueType: 1,
        icon: "",
        valueSelect: "",
        attrType: 1,
        enable: 1,
        catelogId: "",
        attrGroupId: "",
        showDesc: 0
      },
      visible: false,
      dataRule: {

      }
    }
  },
  methods: {
    init(id) {

      this.dataForm.attrId = id || 0
      this.visible = true
      this.dataForm.attrType = this.type
      this.$nextTick(()=> {
        this.$refs.dataForm.resetFields()
        if( this.dataForm.attrId) {
          attrService.info(this.dataForm.attrId).then(({data})=>{
            console.log(data)
            if( data && data.code === 0) {
              this.dataForm.attrName = data.attr.attrName
              this.dataForm.searchType = data.attr.searchType
              this.dataForm.valueType = data.attr.valueType
              this.dataForm.icon = data.attr.icon
              this.dataForm.valueSelect = data.attr.valueSelect.split(";")
              this.dataForm.attrType = data.attr.attrType
              this.dataForm.enable = data.attr.enable
              this.dataForm.catelogId = data.attr.catelogId
              this.dataForm.showDesc = data.attr.showDesc
              this.catelogPath = data.attr.catelogPath
              this.$nextTick(()=>{
                this.dataForm.attrGroupId = data.attr.attrGroupId;
              })
            }
          })
        }
      })
      
      
    },
    dataFormSubmit(){
      console.log('确定被点击')
      let body = {
        attrId: this.dataForm.attrId || undefined,
        attrName: this.dataForm.attrName,
        searchType: this.dataForm.searchType,
        valueType: this.dataForm.valueType,
        icon: this.dataForm.icon,
        valueSelect: this.dataForm.valueSelect.join(";"),
        attrType: this.dataForm.attrType,
        enable: this.dataForm.enable,
        catelogId: this.dataForm.catelogId,
        attrGroupId: this.dataForm.attrGroupId,
        showDesc: this.dataForm.showDesc
      }
      attrService.saveOrUpdate(body).then(({data})=>{
        console.log(data)
        if(data && data.code === 0) {
          this.$message.success('提交成功')
        }else {
          this.$message({
            message: '提交失败',
            type: "error"
          })
        }
        this.$emit('refreshDataList')
      })
      this.visible = false

    },
    dialogClose() {
      this.catelogPath = []
    }

  },
  // 创建完成,可以访问this对象
  created() {
  },
  // 挂载完成，可以访问dom元素
  mounted() {
  },

  watch: {
    catelogPath(path) {
      //监听到路径变化需要查出这个三级分类的分组信息
      console.log("路径变了", path)
      if(!path) { return ;}
      if ( path && path.length === 3) {
        this.attrGroups = []
        this.dataForm.catelogId = path[path.length - 1]
        attrgroupService.list(this.dataForm.catelogId).then(({data})=>{
          console.log(data)
          if( data && data.code === 0) {
            this.attrGroups = data.page.list
          } else {
            this.$message.error(data.msg)
          }
        })
      }else if( path.length === 0) {
        this.dataForm.catelogId = "";
      }else {
        this.$message.error("请选择正确的分类");
        this.dataForm.catelogId = "";
      }
    }
  },
  calculate(){},
  // 创建之前
  beforeCreate() {
  },
  // 挂载之前
  beforeMount() {
  },
  // 更新之前
  beforeUpdate() {
  },
  // 更新之后
  updated() {
  },
  // 销毁之前
  beforeDestroy() {
  },
  // 销毁之后
  destroyed() {
  },
  // 如果页面有keep-alive缓存功能,这个函数会触发
  activated() {
  },
}
</script>

<style scoped>
  >>> .el-dialog {
    width: 40%;
  }
  >>> .el-form {
    width: 80%;
  }
</style>
